public class List implements LinkedList{

    static class ListNode{
        public int val;
        public ListNode prev;//前驱
        public ListNode next;//后继

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            //第一次插头
            head = last = node;
        }
        else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (last == null){
            head = last = node;
        }
        else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    @Override
    public void addIndex(int index,int data){
        try {
            checkIndex(index);
        }catch (IndexNotLegalException e) {
            e.printStackTrace();
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        //1. 找到index位置
        ListNode cur = FindIndexSubOne(index);
        ListNode node = new ListNode(data);
        //2、开始绑定节点
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }


    private ListNode FindIndexSubOne(int index){
        //找到index前一个位置
        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }
    public void checkIndex(int index) throws IndexNotLegalException{
        if(index < 0 || index > size()){
            throw new IndexNotLegalException("index位置非法！");
        }
    }
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //判空
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.next.val == key){
                //移除
                ListNode del = cur.next;
                cur.next = del.next;
                del.next.prev = cur;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
            }else{
                prev = cur;
            }
            cur = cur.next;
        }
        if(head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int cnt = 0;
        ListNode cur = head;
        while(cur != null){
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head = null;
    }
}
